package com.cloudfast.platform.menu.controller;

import com.cloudfast.common.user.UserUtils;
import com.cloudfast.context.error.CloudException;
import com.cloudfast.context.error.ErrorCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloudfast.common.page.Page;
import com.cloudfast.common.result.ApiResponse;
import com.cloudfast.controller.BaseController;
import com.cloudfast.platform.menu.entity.Menu;
import com.cloudfast.platform.menu.service.MenuService;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 菜单管理
 */
@RestController
@RequestMapping(value = "menu")
@Api(value = "菜单管理", tags = "菜单管理")
public class MenuController extends BaseController<Menu> {

    @Autowired
    private MenuService menuService;

    @Override
    public ApiResponse findOne(Long id) {
        try {
            return ApiResponse.success(this.menuService.findOne(id));
        } catch (Exception e) {
            throw new CloudException(ErrorCode.FIND_ONE_ERROR);
        }
    }

    @Override
    public ApiResponse findPage(Page<Menu> page, @ApiIgnore Menu entity) {
        try {
            return ApiResponse.success(this.menuService.findPage(page, entity));
        } catch (Exception e) {
            throw new CloudException(ErrorCode.FIND_PAGE_ERROR);
        }
    }

    @Override
    public ApiResponse save(Menu entity) {
        try {
            menuService.save(entity);
            return ApiResponse.success("成功");

        } catch (Exception e) {
            throw new CloudException(ErrorCode.SAVE_ERROR);
        }

    }

    @Override
    public ApiResponse delete(Long id) {
        try {
            this.menuService.delete(id);
            return ApiResponse.success("删除成功");
        } catch (Exception e) {
            throw new CloudException(ErrorCode.DELETE_ERROR);
        }
    }

    /**
     * 获取树状菜单
     *
     * @return
     */
    @PostMapping(value = "menuTree")
    @ApiOperation("获取树状菜单")
    public ApiResponse menuTree() {
        // 管理员获取菜单树
        if (UserUtils.hasPermission("admin") || UserUtils.hasPermission("super_admin")) {
            return ApiResponse.success(this.menuService.findMenuTree(-1L, true));
        } else {
            // 普通用户获取菜单树
            return ApiResponse.success(this.menuService.findMenuTree(-1L, UserUtils.getUser().getId(), true));
        }
    }

    @PostMapping(value = "menuTreeSelect")
    @ApiOperation("获取树状菜单select 组件")
    public ApiResponse menuTreeSelect(Long parentId) {
        if (parentId == null) {
            parentId = -1L;
        }
        // 管理员获取菜单树
        if (UserUtils.hasPermission("admin") || UserUtils.hasPermission("super_admin")) {
            return ApiResponse.success(this.menuService.findMenuTree(parentId, true));
        } else {
            // 普通用户获取菜单树
            return ApiResponse.success(this.menuService.findMenuTree(parentId, UserUtils.getUser().getId(), true));
        }
    }

    /**
     * 获取下拉数据
     *
     * @return
     */
    @ApiOperation("下拉菜单")
    @PostMapping(value = "treeData")
    public ApiResponse findMenuTree() {
        return ApiResponse.success(this.menuService.findMenuTree());
    }

    /**
     * 获取树状数据-角色已拥有的菜单
     *
     * @return
     */
    @ApiOperation("查询角色关联的菜单")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id", required = true)})
    @PostMapping(value = "roleTreeData")
    public ApiResponse roleTreeData(Long roleId) {
        return ApiResponse.success(this.menuService.roleTreeData(menuService.findByParentId(-1L).getId(), roleId));
    }

    @PostMapping(value = "menuTreeIds")
    @ApiOperation("角色用户的菜单-返回[1,2,3,4,5]")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id", required = true)})
    public ApiResponse menuTreeIds(Long roleId) {
        // 管理员获取菜单树
        return ApiResponse.success(this.menuService.roleMenuIds(roleId));
    }
}
